package com.xiaomaoguai.meizitu;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @Description :
 * @FileName: PaserURL.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月6日 下午2:39:52
 * @Version:V1.00
 */
public class PaserURL2 {

	private static final String URL = "http://xiuren.com/site/taotutop?c=1001_9_8_13";
	private static String SAVE_PATH = "D://meinv2/";
	private static long num = 0L;
	private final static ExecutorService es = Executors.newFixedThreadPool(10);
	private static final HttpClient httpClient;
	private final static String CONNECTION = "keep-alive";
	private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36";

	static {
		RequestConfig config = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build();
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(config);
		httpClient = httpClientBuilder.build();
	}

	public static void main(String[] args) throws IOException {
		Connection con = Jsoup.connect(URL);
		con.header(HttpHeaders.CONNECTION, CONNECTION);
		con.header(HttpHeaders.USER_AGENT, USER_AGENT);
		Document doc = con.get();
		parser(doc);
		System.out.println("图片张数 ：" + num);
	}

	public static void parser(Document doc) throws IOException {
		Elements imgElements = doc.getElementsByTag("img");
		for (Element element : imgElements) {
			String src = element.attr("src");
			if (src.startsWith("http") && src.endsWith("jpg")) {
				String suffix = ".jpg";
				doTask(src, suffix);
			}
		}
		Elements aElements = doc.getElementsByTag("a");
		for (Element element : aElements) {
			String url = element.attr("href");
			if (url.startsWith("http")) {
				Connection con = Jsoup.connect(url);
				con.header(HttpHeaders.CONNECTION, CONNECTION);
				con.header(HttpHeaders.USER_AGENT, USER_AGENT);
				Document itemDoc = con.get();
				parser(itemDoc);
			}
		}
		for (Element element : aElements) {
			String url = element.attr("href");
			if (url.startsWith("/a")) {
				String joinUrl = URL + url;
				System.out.println("拼接URL:" + joinUrl);
				Connection con = Jsoup.connect(joinUrl);
				con.header(HttpHeaders.CONNECTION, CONNECTION);
				con.header(HttpHeaders.USER_AGENT, USER_AGENT);
				Document itemDoc = con.get();
				parser(itemDoc);
				break;
			}
		}
	}

	public static void doTask(String httpUrl, String suffix) {
		es.execute(new Runner(httpUrl, suffix));
	}

	public static void downImg(String httpUrl, String suffix) throws ClientProtocolException, IOException {
		System.out.println(httpUrl);
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet.setHeader(HttpHeaders.USER_AGENT, USER_AGENT);
		HttpResponse response = httpClient.execute(httpGet);
		int statusCode = response.getStatusLine().getStatusCode();
		if (statusCode != 200) {
			httpGet.abort();
			throw new RuntimeException("HttpClient,error status code :" + statusCode);
		}
		HttpEntity entity = response.getEntity();
		InputStream inputStream = entity.getContent();
		File file = new File(SAVE_PATH + (num++) + suffix);
		FileUtils.copyInputStreamToFile(inputStream, file);
	}

	private static class Runner implements Runnable {

		private String url;

		private String fileName;

		public Runner(String url, String fileName) {
			super();
			this.url = url;
			this.fileName = fileName;
		}

		public void run() {
			try {
				downImg(url, fileName);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}
